<template>
  <Tab @goBack="goBack" :tabs="tabs" :current="current">
    <div slot="index">
      <el-form :inline="true" label-width="80px">
        <el-form-item label="讲师姓名">
          <el-input
            v-model="queryParams.nickname"
            placeholder="讲师姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="是否推荐">
          <el-select @keyup.enter.native="handleQuery" v-model="queryParams.recommend" placeholder="是否推荐" clearable
                     size="small">
            <el-option
              v-for="dict in recommendOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="success" size="mini" @click="handleInsert" v-hasPermi="['college:courseOption:add']">添加
          </el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="postList">
        <el-table-column label="课程名称" align="center" prop="coursename"/>
        <el-table-column label="课件名称" align="center" prop="optionname"/>
        <el-table-column label="课件文件" align="center" prop="coursepath" :show-tooltip-when-overflow="true">
          <template slot-scope="scope">
            <el-link :href="scope.row.coursepath" target="_blank" type="primary">{{scope.row.coursepath}}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="集数(第几集)" align="center" prop="number"/>
        <el-table-column label="播放次数" align="center" prop="playnumber"/>
        <el-table-column label="是否免费" align="center" prop="free">
          <template slot-scope="scope">
            <span class="text-warning" v-if="scope.row.free===0">免费</span>
            <span class="text-navy" v-else>收费</span>
          </template>
        </el-table-column>
        <el-table-column label="讲师" align="center" prop="lecturer"/>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <span v-if="scope.row.status===0">下架</span>
            <span class="text-navy" v-else-if="scope.row.status===1">上架</span>
            <span class="text-danger" v-else>删除</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createtime" :show-tooltip-when-overflow="true"/>
        <el-table-column label="操作" align="center" width="300">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              v-if="scope.row.status===0"
              @click="handleOnline(scope.row)"
              v-hasPermi="['college:courseOption:changeStatus']"
            >上架
            </el-button>
            <el-button
              v-if="scope.row.status===1"
              size="mini"
              type="warning"
              @click="handleOffline(scope.row)"
              v-hasPermi="['college:courseOption:changeStatus']"
            >下架
            </el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['college:courseOption:edit']"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              v-hasPermi="['college:courseOption:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <div slot="add">
      <Add :row="row" @goBack="goBack"></Add>
    </div>
  </Tab>

</template>

<script>
  import {listOption, removeOption, changeStatusCourseOption} from "@/api/college/option";
  import Add from "./add";
  import Tab from "@/components/Tab";

  export default {
    components: {Add, Tab},
    data() {
      return {
        current: 'index',
        row: undefined,
        open: false,
        loading: false,
        postList: [],
        title: '',
        total: 0,
        recommendOptions: [{"dictLabel": "是", "dictValue": "1"}, {"dictLabel": "否", "dictValue": "0"}],
        tabs: [
          {index: 'index', name: ''}, {'index': 'add', name: '新增'}
        ],
        queryParams: {
          pageNum: 0,
          pageSize: 10,
          recommend: undefined,
          nickname: undefined
        }
      }
    },
    created() {
      this.getList();
    },
    methods: {
      handleDelete(row) {
        let _this = this;
        this.$confirm(`确定要删除讲师昵称为[${row.optionname}]的记录?删除将找不回此记录.`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          removeOption(row.id).then(response => {
            _this.msgSuccess("删除成功");
            _this.getList();
          });
        }).catch(function (e) {
          console.log(e);
        });
      },
      recommendFormat(row, column) {
        return this.selectDictLabel(this.recommendOptions, row[column.property]);
      },
      getList() {
        this.loading = true;
        listOption(this.queryDateRange(this.queryParams, this.daterange)).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      handleInsert() {
        this.row = undefined;
        this.$set(this.tabs, 1, {index: 'add', name: '新增附件'});
        this.current = 'add';
      },
      handleUpdate(row) {
        this.row = row;
        this.$set(this.tabs, 1, {index: 'add', name: '编辑附件'});
        this.current = 'add';
      },
      handleOffline(row) {
        this.handleChangeStatus({id: row.id, status: 0,optionname:row.optionname,courseId:row.courseId});
      },
      handleOnline(row) {
        this.handleChangeStatus({id: row.id, status: 1,optionname:row.optionname,courseId:row.courseId});
      },
      handleChangeStatus(row) {
        let _this = this;
        this.$confirm(`确定要改变课件名称为[${row.optionname}]的记录?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "info"
        }).then(function () {
          changeStatusCourseOption(row).then(response => {
            _this.msgSuccess("操作成功");
            _this.getList();
          });
        });
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      goBack() {
        this.current = 'index';
        this.getList();
      }
    }
  }
</script>

<style>

</style>
